#!/bin/bash

currentBranch="$(git rev-parse --abbrev-ref HEAD)"
originCommit="$(git rev-parse --short "$(git merge-base master "$currentBranch")")"
filesToBePushed="$(git diff --name-only "$originCommit")"
failed=0

for chartName in $( cut -d'/' -f1,2 <<< "$filesToBePushed" | uniq ); do
    # Avoid running 'helm lint|install' when modified dirs are not charts
    if [[ $chartName = bitnami/* ]]; then
        printf '\033[01;33mValidating %s:\n\033[0m' "$chartName"
        chartPath="$(git rev-parse --show-toplevel)"/"$chartName"

        printf '\033[0;34m- Running helm lint %s\n\033[0m' "$chartPath"
        if helm lint "$chartPath"; then
            printf '\033[0;32m\U2705 helm lint %s\n\n\033[0m' "$chartPath"
        else
            printf '\033[0;31m\U1F6AB helm lint %s failed. Push cancelled.\n\n\033[0m' "$chartPath"
            failed=1
        fi

        printf '\033\033[0;34m- Running helm template %s\n\033[0m' "$chartPath"
        helm repo add bitnami https://charts.bitnami.com/bitnami >> /dev/null
        helm dependency update "$chartPath" >> /dev/null
        if helm template "$chartPath" >> /dev/null; then
            printf '\033[0;32m\U2705 helm template %s\n\n\033[0m' "$chartPath"
        else
            printf '\033[0;31m\U1F6AB helm template %s failed. Push cancelled.\n\n\033[0m' "$chartPath"
            failed=1
        fi
    fi
done

exit $failed
